package si.ibloc.cms.logic.impl;

import java.io.Serializable;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import si.ibloc.cms.logic.interfaces.ContentManager;
import si.ibloc.cms.model.Article;
import si.ibloc.cms.model.Content;
import si.ibloc.cms.model.File;

@Stateful
@Name("contentManager")
@Scope(ScopeType.SESSION)
public class ContentManagerImpl implements ContentManager, Serializable {
   private static final long serialVersionUID = 2618035076975915420L;

   @Logger private Log log;
   @PersistenceContext(type = PersistenceContextType.EXTENDED) private transient EntityManager em;
   @In private FacesMessages facesMessages;

   @DataModel private List<Article> allArticles;
   @DataModelSelection("allArticles") @Out private Article displayedArticle;

   @Out(required = false) private List<File> allFiles;
   @Out(required = false) private String selectedFileContents;

   @Factory("allArticles")
   @Observer("newContentSaved")
   public void findArticles() {
      allArticles = em.createQuery("select a from Article a").getResultList();
      log.info("Got #0 articles.", allArticles.size());
      displayedArticle = allArticles.get(0);
   }

   @Factory("allFiles")
   @Observer("newContentSaved")
   public void findFiles() {
      allFiles = em.createQuery("select f from File f").getResultList();
      log.info("Got #0 files.", allFiles.size());
   }

   public void selectArticle() { }

   public String selectFile(File file) {
      if (file.getContentType() == null || !file.getContentType().startsWith("text/")) {
         facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_WARN, "msg.file.contentType.unknown", file.getContentType());
         return null;
      }
      selectedFileContents = new String(file.getFileContents());
      return "show-file";
   }

   public String selectContent(Content content) {
      if (content instanceof Article) {
         displayedArticle = (Article)content;
         return "home";
      } else if (content instanceof File) {
         return selectFile((File)content);
      }
      facesMessages.add(FacesMessage.SEVERITY_ERROR, "Unknown Content: #0", content);
      return null;
   }

   @End @Remove public void destroy() { }
}
